package com.palfund.eventbus;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class MainActivity extends AppCompatActivity {
    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 1, 1, "register");
        menu.add(Menu.NONE, 2, 1, "unregister");
        menu.add(Menu.NONE, 3, 1, "post");
        menu.add(Menu.NONE, 4, 1, "postSticky");
        menu.add(Menu.NONE, 5, 1, "CancelSticky");
        menu.add(Menu.NONE, 6, 1, "Jump");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                // 注册,首先通过注解获取到订阅者中所有的事件处理方法的集合List<SubscriberMethod>,然后遍历
                // 事件处理方法的集合进行订阅(利用订阅者和事件处理方法创建订阅对象subscription,首先从
                // HashMap：Map<Class<?>,
                // CopyOnWriteArrayList<Subscription>>subscriptionsByEventType事件-订阅集合 中根据事件eventType
                // 获取事件订阅的集合CopyOnWriteArrayList<Subscription> subscriptions,如果集合为null则说明此事件没有添加过,
                // 创建事件订阅集合,并将事件eventType、事件订阅集合CopyOnWriteArrayList<Subscription>
                // subscriptions添加到map中
                // 并将订阅对象subscription添加到事件订阅集合subscriptions中;然后从Map<Object, List<Class<?>>>
                // typesBySubscriber
                // 订阅者-事件集合中,根据订阅者获取事件集合为null,则说明没有添加过,则创建集合并将订阅者、事件集合添加到map中,并将事件添加到事件集合中
                EventBus.getDefault().register(this);
                Log.i("MainActivity", "---onOptionsItemSelected()--->" + "register");
                break;
            case 2:
                // 取消注册
                // 首先订阅者-事件map通过订阅者获取相应的事件集合subscribedTypes,遍历事件集合利用订阅者和事件,
                // 从事件-订阅集合map中根据事件获取到订阅集合list,遍历订阅集合如果订阅的订阅者和传入的订阅者相同,则从订阅集合list中移除此订阅
                EventBus.getDefault().unregister(this);
                Log.i("MainActivity", "---onOptionsItemSelected()--->" + "unregister");
                break;
            case 3:
                // post注册后发送消息
                EventBus.getDefault().post(new MessageEvent("post"));
                Log.i("MainActivity", "---onOptionsItemSelected()--->" + "post");
                break;
            case 4:
                // postSticky()在内存中保存最新的消息,取消原有消息,执行最新消息,只有在注册后才会执行,如果没有注册,消息会一直保留来内存中.
                // stickyEvents.put(event.getClass(), event);
                EventBus.getDefault().postSticky(new MessageEvent("postSticky" + (count++)));
                Log.i("MainActivity", "---onOptionsItemSelected()--->" + "postSticky");
                break;
            case 5:
                MessageEvent stickyEvent = EventBus.getDefault().getStickyEvent(MessageEvent.class);
                // Better check that an event was actually posted before
                if (stickyEvent != null) {
                    // "Consume" the sticky event
                    EventBus.getDefault().removeStickyEvent(stickyEvent);
                    //or
                    EventBus.getDefault().removeAllStickyEvents();
                    // Now do something with it
                }
                break;
            default:
                startActivity(new Intent(this, NextStickyActivity.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Subscribe(priority = 6)// 优先级只是在相同的ThreadMode中才有效，不同的ThreadMode不起作用
    public void onMessageEvent(MessageEvent message) {
        Toast.makeText(this, "" + message.mMessage, Toast.LENGTH_SHORT).show();
        // 中止事件传递,后续事件不在调用.注意:只能在传递事件的时候调用
        //EventBus.getDefault().cancelEventDelivery(message);
        Log.i("MainActivity", "---onMessageEvent()--->" + message.mMessage);
    }

    @Subscribe(sticky = true)
    public void onStickyMessageEvent(MessageEvent message) {
        Toast.makeText(this, "" + message.mMessage, Toast.LENGTH_SHORT).show();
        Log.i("MainActivity", "---onStickyMessageEvent()--->" + message.mMessage);
    }
}